package com.zhaodui.pa.service.impl;
import com.zhaodui.pa.service.FxjPaMainServiceI;
import org.jeecgframework.core.common.service.impl.CommonServiceImpl;
import com.zhaodui.pa.entity.FxjPaMainEntity;
import com.zhaodui.pa.page.FxjPaMainPage;
import com.zhaodui.pa.entity.FxjPaCarEntity;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import org.jeecgframework.core.common.exception.BusinessException;
import org.jeecgframework.core.common.service.impl.CommonServiceImpl;
import org.jeecgframework.core.util.MyBeanUtils;
import org.jeecgframework.core.util.StringUtil;
import org.jeecgframework.core.util.oConvertUtils;
import org.jeecgframework.core.util.ApplicationContextUtil;
import org.jeecgframework.core.util.MyClassLoader;
import org.jeecgframework.web.cgform.enhance.CgformEnhanceJavaInter;
import java.util.ArrayList;
import java.util.UUID;
import java.io.Serializable;

import java.util.Map;
import java.util.HashMap;
import org.jeecgframework.minidao.util.FreemarkerParseFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.jeecgframework.core.util.ResourceUtil;

@Service("fxjPaMainService")
@Transactional
public class FxjPaMainServiceImpl extends CommonServiceImpl implements FxjPaMainServiceI {

	@Autowired
	private NamedParameterJdbcTemplate namedParameterJdbcTemplate;
	
 	public void delete(FxjPaMainEntity entity) throws Exception{
 		super.delete(entity);
 	}
	
	public void addMain(FxjPaMainPage fxjPaMainPage) throws Exception {
		FxjPaMainEntity fxjPaMain = new FxjPaMainEntity();
		MyBeanUtils.copyBeanNotNull2Bean(fxjPaMainPage, fxjPaMain);
		//保存主信息
		this.save(fxjPaMain);
		/**保存-车辆信息*/
		List<FxjPaCarEntity> fxjPaCarList = fxjPaMainPage.getFxjPaCarList();
		for(FxjPaCarEntity fxjPaCar:fxjPaCarList){
			//外键设置
			fxjPaCar.setPaNo(fxjPaMain.getPaNo());
			this.save(fxjPaCar);
		}
	}

	
	public void updateMain(FxjPaMainPage fxjPaMainPage) throws Exception{
		FxjPaMainEntity fxjPaMain = new FxjPaMainEntity();
		//保存主表信息
		if(StringUtil.isNotEmpty(fxjPaMainPage.getId())){
			fxjPaMain = findUniqueByProperty(FxjPaMainEntity.class, "id", fxjPaMainPage.getId());
			MyBeanUtils.copyBeanNotNull2Bean(fxjPaMainPage, fxjPaMain);
			this.saveOrUpdate(fxjPaMain);
		}else{
			this.saveOrUpdate(fxjPaMain);
		}
		//===================================================================================
		//获取参数
		Object paNo0 = fxjPaMain.getPaNo();
		//===================================================================================
		//1.车辆信息数据库的数据
	    String hql0 = "from FxjPaCarEntity where 1 = 1 AND paNo = ? ";
	    List<FxjPaCarEntity> fxjPaCarOldList = this.findHql(hql0,paNo0);
		//2.车辆信息新的数据
		List<FxjPaCarEntity> fxjPaCarList = fxjPaMainPage.getFxjPaCarList();
	    //3.筛选更新明细数据-车辆信息
		if(fxjPaCarList!=null &&fxjPaCarList.size()>0){
			for(FxjPaCarEntity oldE:fxjPaCarOldList){
				boolean isUpdate = false;
				for(FxjPaCarEntity sendE:fxjPaCarList){
					//需要更新的明细数据-车辆信息
					if(oldE.getId().equals(sendE.getId())){
		    			try {
							MyBeanUtils.copyBeanNotNull2Bean(sendE,oldE);
							this.saveOrUpdate(oldE);
						} catch (Exception e) {
							e.printStackTrace();
							throw new BusinessException(e.getMessage());
						}
						isUpdate= true;
		    			break;
		    		}
		    	}
	    		if(!isUpdate){
		    		//如果数据库存在的明细，前台没有传递过来则是删除-车辆信息
		    		super.delete(oldE);
	    		}
	    		
			}
			//4.持久化新增的数据-车辆信息
			for(FxjPaCarEntity fxjPaCar:fxjPaCarList){
				if(oConvertUtils.isEmpty(fxjPaCar.getId())){
					//外键设置
					fxjPaCar.setPaNo(fxjPaMain.getPaNo());
					this.save(fxjPaCar);
				}
			}
		}
	}

	public void delMain(FxjPaMainEntity fxjPaMain) throws Exception{
		//删除主表信息
		this.delete(fxjPaMain);
		//===================================================================================
		//获取参数
		Object paNo0 = fxjPaMain.getPaNo();
		//===================================================================================
		//删除-车辆信息
	    String hql0 = "from FxjPaCarEntity where 1 = 1 AND paNo = ? ";
	    List<FxjPaCarEntity> fxjPaCarOldList = this.findHql(hql0,paNo0);
		this.deleteAllEntitie(fxjPaCarOldList);
	}
 	
}